package cn.lhn.ridersclub.controller;

import cn.lhn.ridersclub.anno.TokenVerify;
import cn.lhn.ridersclub.dto.ChatMessageDto;
import cn.lhn.ridersclub.dto.MsgListDto;
import cn.lhn.ridersclub.dto.Result;
import cn.lhn.ridersclub.entity.Message;
import cn.lhn.ridersclub.service.BlacklistService;
import cn.lhn.ridersclub.service.MessageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController()
@RequestMapping("/message")
public class MessageController {

    private final MessageService messageService;

    private final BlacklistService blacklistService;

    public MessageController(MessageService messageService, BlacklistService blacklistService) {
        this.messageService = messageService;
        this.blacklistService = blacklistService;
    }

    @PostMapping("/send")
    @TokenVerify
    public Result<?> sendMessage(Long targetId, String content, HttpServletRequest request) {
        Long sourceId = Long.parseLong(request.getHeader("arg0"));
        messageService.add(sourceId, targetId, content);
        return Result.success().message("发送成功");
    }

    @GetMapping("/listMsgList")
    @TokenVerify
    //  查询消息列表
    public Result<List<MsgListDto>> listMsgList(HttpServletRequest request) {
        Long accountId = Long.parseLong(request.getHeader("arg0"));
        List<MsgListDto> messages = messageService.listMsgList(accountId);
        return Result.success("查询成功", messages);
    }

    @PostMapping("/setRead")
    @TokenVerify
    public Result<?> setRead(Long targetId, HttpServletRequest request) {
        long sourceId = Long.parseLong(request.getHeader("arg0"));
        messageService.setRead(sourceId, targetId);
        return Result.success();
    }

    @PostMapping("/setReadAll")
    @TokenVerify
    public Result<?> setReadAll(HttpServletRequest request) {
        long sourceId = Long.parseLong(request.getHeader("arg0"));
        messageService.setReadAll(sourceId);
        return Result.success();
    }

    @GetMapping("/listMsgForTarget")
    @TokenVerify
    public Result<List<ChatMessageDto>> listMsgForTarget(Long targetId, Long startId, HttpServletRequest request) {
        List<ChatMessageDto> list = messageService.listMsgForTarget(targetId, startId, Long.parseLong(request.getHeader("arg0")));
        long end = System.currentTimeMillis();
        return Result.success(list);
    }

    @GetMapping("/listNewMsg")
    public Result<List<ChatMessageDto>> listNewMsg(Long targetId, Long endId, HttpServletRequest request) {
        List<ChatMessageDto> list = messageService.listNewMsg(targetId, endId, Long.parseLong(request.getHeader("arg0")));
        return Result.success(list);
    }

    @PostMapping("/deleteMsgListByTarget")
    @TokenVerify
    public Result<?> deleteMsgListByTarget(Long targetId, HttpServletRequest request) {
        messageService.deleteMsgListByTarget(targetId, Long.parseLong(request.getHeader("arg0")));
        return Result.success();
    }

    @GetMapping("/allMessages")
    @TokenVerify
    public Result<List<Message>> getAllMessages(HttpServletRequest request) {
        Long accountId = Long.parseLong(request.getHeader("arg0"));
        List<Message> messages = messageService.getAllMessages(accountId);
        return Result.success("查询成功", messages);
    }

    @GetMapping("/getMessageByTargetId")
    @TokenVerify
    public Result<List<Message>> getAllMessageByTargetId(Long targetId, HttpServletRequest request) {
        Long accountId = Long.parseLong(request.getHeader("arg0"));
        List<Message> list = messageService.getAllMessageByTargetId(targetId, accountId);
        return Result.success("查询成功", list);
    }
}
